package com.chenfy.ioc_api;

import android.app.Activity;

import com.chenfy.ioc_annotation.ViewInject;

import java.util.concurrent.atomic.AtomicReference;

/**
 * Created by ChenFengyao
 * Date: 20-11-11
 */
public class Binding {
    private static final String PACKAGE_NAME = "com.chenfy.iocdemo";
    private static final String CLASS_NAME = "BindHelper";
    private static final AtomicReference<Binding> INSTANCE = new AtomicReference<>();
    private ViewInject mViewInject;

    public static Binding getInstance() {
        for (; ; ) {
            Binding current = INSTANCE.get();
            if (current != null) {
                return current;
            }
            current = new Binding();
            if (INSTANCE.compareAndSet(null, current)) {
                return current;
            }
        }
    }

    private Binding() {
        String bindingClassName = PACKAGE_NAME + "." + CLASS_NAME;//com.dsh.txlessons.annotaionprocessing.AptActivityBinding
        try {
            Class bindingClass = Class.forName(bindingClassName);
            Object o = bindingClass.newInstance();
            if (o instanceof ViewInject){
                mViewInject = (ViewInject) o;
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public void bind(Activity activity){
        mViewInject.inject(activity);
    }
}
